package schoolmath.web.services;

import org.apache.shiro.realm.Realm;
import org.apache.tapestry5.SymbolConstants;
import org.apache.tapestry5.ioc.Configuration;
import org.apache.tapestry5.ioc.MappedConfiguration;
import org.apache.tapestry5.ioc.ServiceBinder;
import org.apache.tapestry5.ioc.annotations.Startup;
import org.apache.tapestry5.services.javascript.JavaScriptStack;
import org.tynamo.security.SecuritySymbols;
import schoolmath.web.services.stacks.BootstrapStack;
import schoolmath.web.services.stacks.BootstrapStackImpl;
import schoolmath.web.services.valueEncoders.*;

public class SchoolmathModule {

	public static void bind(ServiceBinder binder) {
		binder.bind(BootstrapStack.class, BootstrapStackImpl.class);
		binder.bind(SchoolmathRealm.class, SchoolmathRealmImpl.class);
		binder.bind(LiquiBaseConnector.class, LiquiBaseConnectorImpl.class);
		binder.bind(UserEncoder.class, UserEncoderImpl.class);
		binder.bind(RoleEncoder.class, RoleEncoderImpl.class);
		binder.bind(UserGroupEncoder.class, UserGroupEncoderImpl.class);
		binder.bind(UserGroupUnitEncoder.class, UserGroupUnitEncoderImpl.class);
		binder.bind(PresentationEncoder.class, PresentationEncoderImpl.class);
		binder.bind(SelectService.class, SelectServiceImpl.class);
		binder.bind(UserService.class, UserServiceImpl.class);
		binder.bind(RoleService.class, RoleServiceImpl.class);
	}

	public void contributeWebSecurityManager(Configuration<Realm> configuration, SchoolmathRealm realm) {
		configuration.add(realm);
	}

	public void contributeApplicationDefaults(MappedConfiguration<String, String> configuration) {
		configuration.add(SymbolConstants.HMAC_PASSPHRASE, "fwerwerweraesfasdfawer");
		configuration.add(SymbolConstants.START_PAGE_NAME, "panel");
		configuration.add(SecuritySymbols.LOGIN_URL, "/signin");
		configuration.add(SecuritySymbols.UNAUTHORIZED_URL, "/blocked");
		configuration.add(SecuritySymbols.SUCCESS_URL, "/panel");
	}

	public void contributeJavaScriptStackSource(MappedConfiguration<String, JavaScriptStack> configuration, BootstrapStack bootstrapStack) {
		configuration.add("bootstrap", bootstrapStack);
	}

	@Startup
	public void consistDatabase(LiquiBaseConnector liquibaseConnector) {
		liquibaseConnector.update();
	}

}
